package com.utils;

import com.alibaba.fastjson.JSONObject;
import com.common.GmallConfig;
import com.google.common.base.CaseFormat;
import com.google.gson.JsonObject;
import org.apache.commons.beanutils.BeanUtils;


import java.lang.reflect.InvocationTargetException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: TODO QQ1667847363
 * @author: xiao kun tai
 * @date:2022/1/5 23:27
 */
public class JdbcUtils {
    public static <T> List<T> queryList(Connection connection, String querySql, Class<T> clz, boolean underScoreToCamel) throws SQLException, IllegalAccessException, InstantiationException, InvocationTargetException {

        //创建集合用于存放查询结果数据
        ArrayList<T> resultList = new ArrayList<>();

        //预编译SQL
        PreparedStatement preparedStatement = connection.prepareStatement(querySql);

        //执行查询
        ResultSet resultSet = preparedStatement.executeQuery();

        //解析结果集resultSet
        ResultSetMetaData metaData = resultSet.getMetaData();
        int columnCount = metaData.getColumnCount();
        while (resultSet.next()) {
            //创建泛型对象
            T t = clz.newInstance();
            //给泛型对象赋值
            for (int i = 1; i <= columnCount; i++) {

                //获取列名
                String columnName = metaData.getColumnName(i);

                //判断是否需要转换为驼峰命名
                if (underScoreToCamel) {
                    columnName = CaseFormat.LOWER_UNDERSCORE
                            .to(CaseFormat.LOWER_CAMEL, columnName);
                }

                //获取列值
                Object value = resultSet.getObject(i);

                //给泛型对象赋值
//                BeanUtils.copyProperty(t,columnName,value); // TODO:JsonObject =>{} 无论有没有对象都为{}

                BeanUtils.setProperty(t,columnName,value);


            }

            //将该对象添加至集合
            resultList.add(t);

        }
        preparedStatement.close();
        resultSet.close();


        //返回结果集合
        return resultList;

    }

    public static void main(String[] args) throws ClassNotFoundException, SQLException, IllegalAccessException, InvocationTargetException, InstantiationException {
        System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "aa_bb"));


        Class.forName(GmallConfig.PHOENIX_DRIVER);
        Connection connection = DriverManager.getConnection(GmallConfig.PHOENIX_SERVER);

        List<JSONObject> queryList = queryList(connection,
                "select * from GMALL_REALTIME.DIM_USER_INFO",
                JSONObject.class,
                false);
        for (JSONObject jsonObject : queryList) {
            System.out.println(jsonObject.toJSONString());
        }
        connection.close();

    }
}
